﻿using System.Net;
using System.Web.UI;
using ReadyApps.Wiki.Templates;

namespace ReadyApps.Wiki.Views
{
    class NotAuthorizedView : ViewBase, IViewFiresTitleSetEvent
    {
        #region Variables and Constants

        public const string NOT_AUTHORIZED_TITLE = "Not Authorized";

        #endregion

        #region Constructor

        public NotAuthorizedView(WikiControlSettings wikiControlSettings)
            : base(wikiControlSettings) { }

        #endregion

        #region Public Methods

        public override void BuildControlTree(ControlCollection controls, bool raiseEvents)
        {
            Control container = new Control();

            // Instantiate the template
            ITemplate notAuthorizedTemplate = controlSettings.PageNotAuthorizedTemplate ?? new WikiDefaultNotAuthorizedTemplate();
            notAuthorizedTemplate.InstantiateIn(container);

            // Set the status code
            if (raiseEvents)
            {
                controlSettings.OnTitleSet(this, new TitleSetEventArgs(NOT_AUTHORIZED_TITLE));
                controlSettings.OnStatusChange(this, new WikiPageStatusEventArgs(HttpStatusCode.Unauthorized));
            }

            controls.Clear();
            controls.Add(container);
        }

        #endregion
    }
}
